<template>
  <div class="my-comment-con">
    <div class="empty-con" v-if="isEmpry">
      <div class="empty-text">暂无评论</div>
    </div>
    <div v-else>
      <div class="type-choose">
        <div class="type-con" @click="typeVisible = true">类型选择</div>
      </div>
      <mt-actionsheet :actions="typeActions" v-model="typeVisible">
      </mt-actionsheet>
      <div class="block" v-for="(item, index) in reverseList" :key="index">
        <div class="comment-detail">
          <div class="left-con">
            {{ item.username }}:&nbsp;{{ item.content }}
          </div>
          <div>{{ item.add_time | dataFormat }}</div>
        </div>
        <div class="detail-work">
          {{ item.type | typeFormat }}:&nbsp;{{ item.work_title }}
          <div class="operate">
            <div class="del" @click="delMyComment(item.id, item.content)">
              删除
            </div>
            <div class="see" @click="toInfoDetail(item.work_id, item.type)">
              查看
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      isEmpry: false,
      username: "",
      typeVisible: false,
      typeActions: [
        {
          id: 0,
          name: "全部",
          method: this.select,
        },
        {
          id: 1,
          name: "新闻类",
          method: this.select,
        },
        {
          id: 2,
          name: "图片类",
          method: this.select,
        },
        {
          id: 3,
          name: "商品类",
          method: this.select,
        },
      ],
      type: "",
      myCommentList: [],
    };
  },
  filters: {
    typeFormat(value) {
      switch (value) {
        case 1:
          return "新闻类";
        case 2:
          return "图片类";
        case 3:
          return "商品类";
        default:
          break;
      }
    },
  },
  created() {
    this.username = window.JSON.parse(
      localStorage.getItem("UserInfo")
    ).username;
  },
  activated() {
    this.getMyComment();
  },
  computed: {
    reverseList() {
      return this.myCommentList.reverse();
    },
  },
  methods: {
    getMyComment() {
      this.$http
        .get("/getMyComment", {
          params: {
            username: this.username,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            const data = res.data.message;
            this.myCommentList = data;
          } else {
            this.myCommentList = [];
          }
        });
    },
    // 选择类别
    select(item) {
      this.type = item.id;
      this.typeVisible = false;
      this.$http
        .get("/getMyCommentByType", {
          params: {
            username: this.username,
            type: this.type,
          },
        })
        .then((res) => {
          if (res.data.errcode === 200) {
            const data = res.data.message;
            this.myCommentList = data;
          } else {
            this.myCommentList = [];
          }
        });
    },
    toInfoDetail(work_id, type) {
      // 首先判断是什么类别，然后跳到哪个区域去
      switch (type) {
        case 1:
          this.$router.push({
            path: "/idssp/news/newsinfo/" + work_id,
          });
          break;
        case 2:
          this.$router.push({
            path: "/idssp/photos/photoinfo/" + work_id,
          });
          break;
        case 3:
          this.$router.push({
            path: "/idssp/shops/goodsinfo/" + work_id,
          });
          break;
        default:
          break;
      }
    },
    delMyComment(id, content) {
      this.$messagebox
        .confirm("", {
          message: "是否删除" + content + "评论？",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
        })
        .then((action) => {
          if (action == "confirm") {
            //确认的回调
            let params = {
              id: id,
            };
            this.$http.post("/delMyComment", params).then((res) => {
              if (res.data.errcode === 200) {
                Toast("删除成功!!!");
                this.getMyComment();
              } else {
                Toast("删除失败!!!");
              }
            });
          }
        })
        .catch((err) => {
          if (err == "cancel") {
            //取消的回调
          }
        });
    },
  },
};
</script>
<style lang='scss' scoped>
.my-comment-con {
  font-size: 13px;
  background-color: #eee;
  overflow: hidden;
  padding: 10px 10px 0 10px;
  .empty-con {
    width: 200px;
    height: 200px;
    background: url("../../assets/img/order/empty.png") no-repeat;
    background-size: 100% 100%;
    background-position-y: center;
    margin: 0 auto;
    text-align: center;
    .empty-text {
      padding-top: 180px;
      font-size: 15px;
    }
  }
  .type-choose {
    padding-left: 16px;
    padding-top: 10px;
    width: 100%;
    // height: 10px;
    background-color: #fff;
    .type-con {
      width: 25%;
      text-align: center;
      line-height: 30px;
      border: 1px solid gray;
      background-color: #fff;
      border-radius: 15px;
      // margin-bottom: 10px;
    }
  }
  .block {
    // margin-bottom: 10px;
    padding: 10px 10px;
    background-color: #fff;
    .comment-detail {
      padding: 0 5px;
      border-radius: 15px;
      background-color: #eee;
      display: flex;
      width: 100%;
      justify-content: space-between;
    }
    .detail-work {
      margin-top: 5px;
      padding: 10px 10px;
      border: 1px solid #eee;
      border-radius: 15px;
      .operate {
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
        text-align: center;
        // div {
        //   width: 55px;
        //   border-radius: 15px;
        //   color: white;
        // }
        div {
          border: 1px solid #ccc;
          padding: 5px;
          border-radius: 20px;
          margin-right: 10px;
          width: 80px;
          height: 30px;
          text-align: center;
        }
        .see {
          border: 1px solid orange;
          color: orange;
        }
      }
    }
  }
}
</style>